<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="css/template.css">
		<link rel="stylesheet" type="text/css" href="css/TestGrading.css">
		<link rel="stylesheet" type="text/css" href="css/smoothness/jquery-ui-1.10.0.custom.css" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<script type="text/javascript" src="JQuery/jquery-1.9.0.min.js"></script>
		<script type="text/javascript" src="JQuery/jquery-ui-1.10.0.custom.js"></script>	
		<script type="text/javascript" src="JS/TestGrading.js"></script>
		
		<title>Page Title</title>
	</head>
	<body>
		<?php
		require("webservices.php");
        
		// Grab bacic information about test
		require_once('session_handler.php');
        session_handler_start();
		
		// Check if the user already login
		if(!isset($_SESSION['userid'])) 
		{
			header('Location: firstpage.php?field=LoginFirst');
			exit();
		}
		
		Auto_grade_answer($_GET['trid']);

		?>
		<div id="main">
			
			<div id="TopBoard">
				<img src="images/redpen-01.png" alt="RedPenLogo" id="RedPenLogo"/>
				<span id="AuthInfo">
					<span id="Avatar">
						<?php
							echo '<img src="images/avatar/' . $_SESSION['avatar'] .'" id="AvatarImg" />';
						?>
					</span> 
					<span id="UserName">
					<?php
						echo $_SESSION['username'];
					?>
					</span>
					<span id="Power" style="display: none">
					</span>
					<span id="LogOut"><a href="logout.php" id="LogoutLink">Logout</a></span>
				</span>
					
			</div>
			
			<div id="HeaderBar"></div>	
				
			<div id="Content">
				<div id="TestRecInfo" class="TestPart">
					<div class="TestPartContainer">
					<?php
						$testinfo = Get_test_record_info($_GET['trid'], $_SESSION['userid']);
			
			
						if(array_key_exists('ErrorMsg', $testinfo[0]))
						{
							echo '<img src="images/error.png" class="ErrorIcon" /><span class="ErrorMsg">' . $testinfo[0]['ErrorMsg'] . '</span>';
							exit();
						}
						else
						{
						
							$CourseName  = $testinfo[0]['CourseName'];
							$TestName 	 = $testinfo[0]['name'];
							$Description = $testinfo[0]['description'];
							$StudentName = $testinfo[0]['StudentName'];
							$Pledge = $testinfo[0]['Pledge'];
							$StudentPledge = $testinfo[0]['StudentPledge'];
							$MaxPoints   = $testinfo[0]['max_points'];
						
						echo '<span id="TestName">' . $TestName . '</span>';
						echo '<br/><br/>';
						echo '<span class="TestInfoLabel">Course</span><span class="TestInfoValue">' . $CourseName . '</span>';
						echo '<br/>';
						echo '<span class="TestInfoLabel">Student Name</span><span class="TestInfoValue">' . $StudentName . '</span>';
						echo '<br/>';
						echo '<span class="TestInfoLabel">Maximum Points</span><span class="TestInfoValue">' . $MaxPoints . '</span>';
						echo '<br/>';
						echo '<span class="TestInfoLabel">Description</span><span class="TestInfoValue">' . $Description . '</span>';
						}
					?>
					</div>
					</div>		
				
				<form id="TestForm" action="GradeTest.php" method="post">
				<div id="StudentPledge" class="TestPart">
				<div class="TestPartContainer">
					<h3 id="PledgeTitle" class="header">Pledge</h3>
					<?php
						echo '<div id="PledgeStatement">';
						echo $Pledge;
						echo '</div>';
						
						echo '<br/><span class="bold">Student pledge: </span>';
						echo '<br/><br/><span id="studentpledge">"' . $StudentPledge . '"</span>';
				
					?>
					<br/><br/>
					<input type="checkbox" id="ValidPledgeChBx" /><span id="ValidPledgeLabel">Valid</span>
				</div>
				</div>
				
				<div id="TestQuestions" class="TestPart">
					<div class="TestPartContainer">
						
				<?php
					$Questions = get_student_test_answers($_GET['trid']);
					$Questions = get_student_test_answers($_GET['trid'], $_SESSION['userid']);
					$TrueFalse = false;
					$MultipleChoice = false;
					$ShortAnswer = false;
					$Essay = false;
					$Matching = false;
					$ChooseAll = false;
					$QuestionNumber = 1;
					
					
					echo '<input type="text" id="TestRecordID" class="hidden" name="RecordID" value="' . $_GET['trid'] . '" />';
					
					foreach($Questions as $Question)
					{
						if($Question['QType'] == 1)
						{	
							if(!$TrueFalse)
							{
								echo '<br/><span class="TestSectionHeader">True or False</span>';
								$TrueFalse = true;
								$MultipleChoice = false;
								$ShortAnswer = false;
								$Essay = false;
								$Matching = false;
								$ChooseAll = false;
							}	
							echo '<br/><br/><span class="QuestionNumber">' . $QuestionNumber . '. </span>';
							echo '<span class="QuestionText">' . $Question['Question'] . ' (' . $Question['Points'] . ')</span>';
							echo '<br/><span class="answers">';
							
							$Tinput = '<input class="OptionRadio UserAnswerOption" disabled="disabled" type="radio" name="' . $Question['id'] . '" value="1" ';
							$Finput = '<br/><input class="OptionRadio UserAnswerOption" disabled="disabled" type="radio" name="' . $Question['id'] . '" value="2" ';

							$SpanTF = '<span class="TFLabel ';
							$TSpan = $SpanTF;
							$FSpan = $SpanTF;
							
							if ($Question['CorrectAnswer'] == 1)
							{
								$TSpan .= ' CorrectAnswer" >';
								$FSpan .= '" >';
							}
							else if ($Question['CorrectAnswer'] == 2)
							{
								$FSpan .= ' CorrectAnswer" >';
								$TSpan .= '" >';
							}
																								
							if($Question['user_answer'] == 1)
							{
								echo $Tinput .  'checked />' . $TSpan . ' True</span>';
								echo $Finput . ' />' . $FSpan . ' False</span>'; 
							}
							else
							{
								echo $Tinput .  ' />' . $TSpan . ' True</span>' ;
								echo $Finput . 'checked />' . $FSpan . ' False</span>';
							}

							echo '</span>';
						
						}
						else if ($Question['QType'] == 2)
						{
							if(!$MultipleChoice)
							{
								echo '<br/><br/><span class="TestSectionHeader">Multiple Choice</span>';
								$MultipleChoice = true;
								$TrueFalse = false;
								$ShortAnswer = false;
								$Essay = false;
								$Matching = false;
								$ChooseAll = false;
							}	
							echo '<br/><br/><span class="QuestionNumber">' . $QuestionNumber . '. </span>';
							echo '<span class="QuestionText">' . $Question['Question'] . ' (' . $Question['Points'] . ')</span>';
							$Options = explode(',', $Question['options']);
							$Options_id = explode(',', $Question['options_id']);
							echo '<br/><span class="answers">';
											
							foreach($Options as $Index => $Option)
							{
								$MCRadio = '<input class="OptionRadio" disabled="disabled" type="radio" name="' . $Question['id'] . '" value="'. $Options_id[$Index] . '" ';
								$MCSpan = '<span class="MCLabel ';	
							
								if($Options_id[$Index] == $Question['CorrectAnswer'])
									$MCSpan .= 'CorrectAnswer" >';
								else
									$MCSpan .= '" >';
								
								if($Options_id[$Index] == $Question['user_answer'])
								{																	
									$MCRadio .= ' checked /> ';
								}
								else 
								{
									$MCRadio .= ' /> '; 
								}
								
								echo $MCRadio . $MCSpan . $Option . '</span><br />';
							}
							echo '</span>';
							
							
						}
						else if ($Question['QType'] == 4)
						{
							if(!$ChooseAll)
							{
								echo '<br/><br/><span class="TestSectionHeader">Choose All That Apply</span>';
								$ChooseAll = true;
								$ShortAnswer = false;
								$MultipleChoice = false;
								$TrueFalse = false;
								$Essay = false;
								$Matching = false;
							}	
							echo '<br/><br/><span class="QuestionNumber">' . $QuestionNumber . '. </span>';
							echo '<span class="QuestionText">' . $Question['Question'] . ' (' . $Question['Points'] . ')</span>';
							$Options = explode(',', $Question['options']);
							$Options_id = explode(',', $Question['options_id']);
							$IsCorrect = explode(',', $Question['CorrectAnswer']);
							
							echo '<br/><span class="answers">';
												
							foreach($Options as $Index => $Option)
							{
								$MCRadio = '<input class="OptionChx" disabled="disabled" type="checkbox" name="' . $Question['id'] . '" value="'. $Options_id[$Index] . '" ';
								$MCSpan = '<span class="Label ';
							
								if($IsCorrect[$Index] == 1)
									$MCSpan .= 'CorrectAnswer" >';
								else
									$MCSpan .= '" >';
									
								if($Options_id[$Index] == $Question['user_answer'])
								{																	
									$MCRadio .= ' checked /> ';
								}
								else 
								{
									$MCRadio .= ' /> '; 
								}
								
								echo $MCRadio . $MCSpan . $Option . '</span><br />';					
							}
							echo '</span>';
						}
						else if ($Question['QType'] == 5)
						{
							if(!$ShortAnswer)
							{
								echo '<br/><br/><span class="TestSectionHeader">Short Answer</span>';
								$ShortAnswer = true;
								$MultipleChoice = false;
								$TrueFalse = false;
								$Essay = false;
								$Matching = false;
								$ChooseAll = false;
							}	
							echo '<br/><br/><span class="QuestionNumber">' . $QuestionNumber . '. </span>';
							echo '<span class="QuestionText">' . $Question['Question'] . ' (' . $Question['Points'] . ')</span>';
							echo '<br/><span class="answers WrittenAnswer">"';
							echo $Question['user_answer'];
							echo '"</span>';
							echo '<br/><span class="answers CorrectShortAnswer">';
							echo 'Correct Answer: ' . '<span class="CorrectAnswer">' . $Question['CorrectAnswer'] . '</span>';
							echo '</span>';
							

						}					
						else if ($Question['QType'] == 6)
						{
							if(!$Essay)
							{
								echo '<br/><br/><span class="TestSectionHeader">Essay</span>';
								$Essay = true;
								$MultipleChoice = false;
								$TrueFalse = false;
								$ShortAnswer = false;
								$Matching = false;
								$ChooseAll = false;
							}	
							echo '<br/><br/><span class="QuestionNumber">' . $QuestionNumber . '. </span>';
							echo '<span class="QuestionText">' . $Question['Question'] . ' (' . $Question['Points'] . ')</span>';
							echo '<br/><span class="answers WrittenAnswer">"';
							echo $Question['user_answer'];	
							echo '"</span>';
							

						}
						
						echo '<br/><br/><span class="GradingPoints">';
							echo 'Points: ';

							
						echo 'Points: ';
						echo '<select class="PointsBox" name="' . $Question['id'] . '" value="' . $Question['PointsGiven'] . '" >';
						for($x = 0; $x <= $Question['Points']; $x++)
						{
							$Selected = '';
							if($x == $Question['PointsGiven'])
								$Selected = ' selected="selected" ';
						
							echo '<option value="' . $x . '"' . $Selected . '>' . $x . '</option>'; 
						}
						echo '</select>';
						echo ' / ' . $Question['Points'];
						
						echo '</span>';
						
						$QuestionNumber++;
					}
					
					
				?>
					</div>
				</div>
			<!-- 	<div id="TestPledge" class="TestPart">
				<div class="TestPartContainer">
				<span id="PledgeHeader">Pledge</span><br/><br/>
				<?php
					//echo $Pledge;				
				?>
				<br/><br/>
				<span id="PledgeName">
				<span id="PledgeNameLabel">Please type your name</span>
				<input type="text" id="PledgeNameBox" name="PledgeName" />
				</span>
				
				</div>
				</div> -->
				
				<br/><br />
				<div id="Buttons">
				<input type="button" id="SubmitButton" value="Complete Grading" />
				</div>
				</form>
				</div>
				
				
				
			
		
			<div id="footer">
				<div id="HeaderBar"></div>
				<br/>
				<span id="Copyright">
					&#169; Team Flash 2013
					<br/>Pensacola Christian College
				</span>
				<br/>
			</div>		
		</div>	
		<div id="PledgeDialog" class="dialog" title="Confirm pledge" style="display:none;">
				<img src="images/processing.png" class="DialogIcon" /><span class="DialogMessage">You have not marked the pledge as a valid pledge. The student grade will be automatically set to 0. 
				Complete the grading process?</span>
		</div>
	</body>
			
</html>